package com.hznu.ez4life.modules.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.hznu.ez4life.modules.entity.User;
import com.hznu.ez4life.modules.dao.UserDao;
import com.hznu.ez4life.modules.service.UserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author DragonDoor
 * @since 2019-07-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserDao, User> implements UserService {

    @Override
    public User findByOpenid(String openid)
    {
        return this.selectOne(new EntityWrapper<User>().eq("openid",openid));
    }

//    @Override
//    public User bindUser(String openid,User user)
//    {
//        User userInDB = this.findByOpenid(openid);
//        if(userInDB==null)
//        {
//            user.setOpenid(openid);
//            this.insert(user);
//        }else
//        {
//            user.setId(userInDB.getId());
//            this.updateById(user);
//        }
//        return user;
//    }

    @Override
    public User getUserInfoByOpenid(User user,String openid)
    {
        User userInDB = this.findByOpenid(openid);
        if(userInDB==null)
        {
            user.setOpenid(openid);
            this.insert(user);
        }else
        {
            user.setId(userInDB.getId());
            this.updateById(user);
        }
        return this.selectById(user);
    }

    @Override
    public User findById(Integer id)
    {
        User user = this.selectById(id);
        return user;
    }
}
